from core import objects as obj
import xml.dom.minidom
import sys

class element_parser:
	"""Class for parsing element config file in \config"""
	
	def __init__(self, config_file, elements):
		"""Constructor
		config file name if it will changed
		elements list of element's symbol needed"""
		
		self.config_file =config_file
		self.elements = elements
	
	
	def parsing(self):
		"""Parsing function
		return an alphabet"""
		
		alphabet = obj.Alphabet()
		document = xml.dom.minidom.parse(self.config_file)
		elementTags = document.getElementsByTagName('element')
		
		for x, elementTag in enumerate(elementTags):
		
			name = elementTag.getAttribute('name')
			symbol = str(elementTag.getAttribute('symbol'))
			atomicNumber = elementTag.getAttribute('atomicNumber')
			
			massTags = elementTag.getElementsByTagName('mass')
			massMo = float(massTags[0].getAttribute('monoisotopic'))
			massAv = float(massTags[0].getAttribute('average'))
			mass = (massMo, massAv)
			
			isotopes = []#;iso=[]
			#isotopes[massNumber] = (imass,abundance)
			isotopeTags = elementTag.getElementsByTagName('isotope')
			for isotopeTag in isotopeTags:
				massNumber = isotopeTag.getAttribute('massNumber')
				imass = float(isotopeTag.getAttribute('mass'))
				abundance = float(isotopeTag.getAttribute('abundance'))
				isotopes.append(abundance)
			#sort the elements
			#for abundancy in sorted(isotopes):
				#iso.append(abundancy)
			if symbol in self.elements:
				alphabet.add_element(obj.Element(mass[0], isotopes, symbol, name))
			
		return alphabet		
		
	
